<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Index extends Controller_Initial_Page {

	public function action_index()
	{
            if (Auth::instance()->logged_in())
            {
                $content_view = $this->generate_notification_board();
            }
            else
            {
                $content_view = View::factory('toolbar');
                $content_view->set('content', View::factory('index/index'));
            }
            
            $this->template->set('body',$content_view);
	}
        
        public function action_internal_index()
        {
           // $content = array('content'=>$this->generate_notification_board()->render());
           // $this->response->body(json_encode($content));
             $this->response->body($this->generate_notification_board()); 
        }
        
        /**
        * Generates notification board page
        * @return string
        */
        private function generate_notification_board()
        {
            $user_model = Auth::instance()->get_user();
            $amount_invitations = count($user_model->invitations->find_all());
                                            
            $notification_board_view = View::factory('index/notification_board');
            $notification_board_view->set('amount_invitations',$amount_invitations);
        
            $content_view = View::factory('content');
            $content_view->set('content',$notification_board_view);

            
            return $content_view;      
        }
        
        public function action_about()
        {
            $content_view = View::factory('content'); 
            $about_view = View::factory('index/footer/about');
            $template_view = View::factory('index/template');
            
            $template_view->set('menu_selected',1);
            $template_view->set('content', $about_view);
            
            $content_view->set('content', $template_view);
            $this->template->set('body', $content_view);  
        }
        
        public function action_ajax_about()
        {
            $content = View::factory('index/footer/about');
            $this->response->body(json_encode(array('content' => $content->render())));
        }
      
        public function action_privacy()
        {
            $content_view = View::factory('content'); 
            $privacy_view = View::factory('index/footer/privacy');
            $template_view = View::factory('index/template');
            
            $template_view->set('menu_selected',2);
            $template_view->set('content', $privacy_view);
            
            $content_view->set('content', $template_view);
            $this->template->set('body', $content_view);
        }
        
        public function action_ajax_privacy()
        {
            $content = View::factory('index/footer/privacy');
            $this->response->body(json_encode(array('content' => $content->render())));
        }
        
        public function action_terms()
        {
            $content_view = View::factory('content'); 
            $terms_view = View::factory('index/footer/terms');
            $template_view = View::factory('index/template');
            
            $template_view->set('menu_selected',3);
            $template_view->set('content', $terms_view);
            
            $content_view->set('content', $template_view);
            $this->template->set('body', $content_view);
        }
        
        public function action_ajax_terms()
        {
            $content = View::factory('index/footer/terms');
            $this->response->body(json_encode(array('content' => $content->render())));
        }
        
        public function action_help()
        {
            $content_view = View::factory('content'); 
            $help_view = View::factory('index/footer/help');
            $template_view = View::factory('index/template');
            
            $template_view->set('menu_selected',4);
            $template_view->set('content', $help_view);
            
            $content_view->set('content', $template_view);
            $this->template->set('body', $content_view);
        }
        
        public function action_ajax_help()
        {
            $content = View::factory('index/footer/help');
            $this->response->body(json_encode(array('content' => $content->render())));
        }
        
        public function action_contact()
        {
            $user = Auth::instance()->get_user();
            $email = $user ? $user->email : null; 
            
            $content_view = View::factory('content'); 
            $contact_view = View::factory('index/footer/contact');
            $contact_view->set('email', $email);
            
            $template_view = View::factory('index/template');
            
            $template_view->set('menu_selected',5);
            $template_view->set('content', $contact_view);
            
            $content_view->set('content', $template_view);
            $this->template->set('body', $content_view);
        } 
        
        public function action_ajax_contact()
        {
            $user = Auth::instance()->get_user();
            
            if($_POST)
            {
                $validation = Validation::factory($_POST);
                $validation->rule('email', 'not_empty')
                           ->rule('email', 'email')
                           ->rule('subject', 'not_empty')
                           ->rule('content_message', 'not_empty')
                           ->label('email', __('Your e-mail'))
                           ->label('subject', __('Subject'))
                           ->label('content_message', __('Content'));

                if($validation->check())
                {
                    $email = Security::xss_clean($_POST['email']);
                    $subject = Security::xss_clean($_POST['subject']);
                    $content_message = Security::xss_clean($_POST['content_message']);
                    
                    $email_template_view = View::factory('email/template');
                    $email_content_view = View::factory('email/contact_message');
                    $email_content_view->set('email',$email);
                    $email_content_view->set('message',$content_message);
                    $email_content_view->set('ip',$_SERVER['REMOTE_ADDR']);
                    
                    if($user)
                    {
                        $email_content_view->set('user',$user->user_profile->url);
                    }
                    
                    $email_template_view->set('title', $subject);
                    $email_template_view->set('content', $email_content_view);

                    $main_config = Kohana::config('main');

                    $send_email = Email::factory('Wiadomość od użytkownika myDe.sk')
                                  ->message($email_template_view, 'text/html')
                                  ->to('info@coderitual.com')
                                  ->from($main_config->contact_mail, $main_config->contact_name)
                                  ->send();
                    if ($send_email)
                    {
                          $output = array('success'=>true,
                                          'message'=>'Message has been sent');
                    }
                    else
                    {
                        // there is a problem with e-mail send                    
                        Log::instance()->add(Log::ERROR, 'Recovery password message could not be sent');
                        throw new HTTP_Exception_404();
                    }
                 }
                 else
                 {
                     $output = array('success' => 'false', 
                                     'message' => 'Oh no!', 
                                     'errors'  => $validation->errors('validation'));
                 }
                 
                  $this->response->body(json_encode($output));
            }
            else
            {
                $content = View::factory('index/footer/contact');
                $email = $user ? $user->email : null; 
                $content->set('email', $email);
                $this->response->body(json_encode(array('content' => $content->render())));
            }
            
        }
        
        public function action_ajax_get_animation_images()
        {
            $file_model = new model_file();
            $images = $file_model->get_animation_images();
            
            $output = array('content' => $images);
            $this->response->body(json_encode($output));
            
            
        }
        
        

} // End Index